package com.ruoyi.waterPrice.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.business.waterPrice.WiFarmacctY;
import com.ruoyi.common.core.domain.business.waterPrice.WiFarmbookR;
import com.ruoyi.common.core.domain.business.waterPrice.WiFeemodeY;
import com.ruoyi.common.core.domain.business.waterPrice.WiOrgbookR;
import com.ruoyi.common.core.enums.FeeKindEnum;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableSupport;
import com.ruoyi.waterPrice.mapper.WiFarmacctYMapper;
import com.ruoyi.waterPrice.mapper.WiFarmbookRMapper;
import com.ruoyi.waterPrice.mapper.WiFeemodeYMapper;
import com.ruoyi.waterPrice.service.IWiOrgbookRService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 机构缴费流水账Controller
 *
 * @author ps
 * @date 2023-02-17
 */
@RestController
@RequestMapping("/WiOrgbookR")
public class WiOrgbookRController extends BaseController {
    @Autowired
    private IWiOrgbookRService wiOrgbookRService;
    @Autowired
    private WiFarmbookRMapper wiFarmbookRMapper;
    @Autowired
    private WiFeemodeYMapper wiFeemodeYMapper;
    @Autowired
    private WiFarmacctYMapper wiFarmacctYMapper;

    private static final Logger log = LoggerFactory.getLogger(WiOrgbookRController.class);

    /**
     * 查询机构缴费流水账列表
     */
    @RequiresPermissions("waterPrice:WiOrgbookR:list")
    @GetMapping("/list")
    public Object list(WiOrgbookR wiOrgbookR) {
        startPage();
        //判断是按机构还是按农户
        String feeYr = StringUtils.isNotBlank(wiOrgbookR.getFeeYr()) ? wiOrgbookR.getFeeYr() : BusinessUtils.getCurCalendar().get("year") + "";
        String feeKind;
        WiFeemodeY wiFeemodeY = wiFeemodeYMapper.selectWiFeemodeYByModeYr(feeYr);
        if (wiFeemodeY != null && StringUtils.isNotBlank(feeYr) && StringUtils.isNotBlank(wiOrgbookR.getOrgCd())) {
            feeKind = wiFeemodeY.getFeeKind();
            if (FeeKindEnum.FRAM.getCode().equals(feeKind)) {
                List<WiFarmbookR> wiFarmbookRS = wiFarmbookRMapper.selectWiFarmbookRListByOrgCdAndYr(feeYr, wiOrgbookR.getOrgCd());
                int total = wiFarmbookRS.size();
                log.info(">>>>>>>>>>>>>" + total);
                PageDomain pageDomain = TableSupport.buildPageRequest();
                List<WiFarmbookR> collect = wiFarmbookRS.stream().skip((pageDomain.getPageNum() - 1) * pageDomain.getPageSize()).limit(pageDomain.getPageSize()).collect(Collectors.toList());
                for (WiFarmbookR vo : collect) {
                    String farmNm = wiFarmacctYMapper.getFarmNm(vo.getFarmCd());
                    if (StringUtils.isNotBlank(farmNm)) {
                        vo.setFarmNm(farmNm);
                    }
                    String operNm = wiFarmacctYMapper.getOperNm(vo.getOperId());
                    if (StringUtils.isNotBlank(operNm)) {
                        vo.setWriteOper(operNm);
                    }
                }
                //重新封装返回数据
                HashMap<String, Object> resMap = new HashMap<>();
                resMap.put("code", 200);
                resMap.put("msg", "查询成功");
                resMap.put("total", total);
                resMap.put("rows", collect);
                return resMap;
            } else {
                return getDataTable(wiOrgbookRService.selectWiOrgbookRList(wiOrgbookR));
            }
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 导出机构缴费流水账列表
     */
    @RequiresPermissions("waterPrice:WiOrgbookR:export")
    @Log(title = "机构缴费流水账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WiOrgbookR wiOrgbookR) {
        String feeKind;
        String feeYr = StringUtils.isNotBlank(wiOrgbookR.getFeeYr()) ? wiOrgbookR.getFeeYr() : BusinessUtils.getCurCalendar().get("year") + "";
        WiFeemodeY wiFeemodeY = wiFeemodeYMapper.selectWiFeemodeYByModeYr(feeYr);
        feeKind = wiFeemodeY.getFeeKind();
        if (FeeKindEnum.FRAM.getCode().equals(feeKind)) {
            HashMap<String, Object> map = (HashMap<String, Object>) this.list(wiOrgbookR);
            List<WiFarmbookR> list = (List<WiFarmbookR>) map.get("rows");
            ExcelUtil<WiFarmbookR> util = new ExcelUtil<WiFarmbookR>(WiFarmbookR.class);
            util.exportExcel(response, list, "机构缴费流水账数据");
        }else{
            List<WiOrgbookR> list = wiOrgbookRService.selectWiOrgbookRList(wiOrgbookR);
            ExcelUtil<WiOrgbookR> util = new ExcelUtil<WiOrgbookR>(WiOrgbookR.class);
            util.exportExcel(response, list, "机构缴费流水账数据");
        }
    }

    /**
     * 获取机构缴费流水账详细信息
     */
    @RequiresPermissions("waterPrice:WiOrgbookR:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(wiOrgbookRService.selectWiOrgbookRById(id));
    }

    /**
     * 新增机构缴费流水账
     */
    @RequiresPermissions("waterPrice:WiOrgbookR:add")
    @Log(title = "机构缴费流水账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WiOrgbookR wiOrgbookR) {
        return toAjax(wiOrgbookRService.insertWiOrgbookR(wiOrgbookR));
    }

    /**
     * 修改机构缴费流水账
     */
    @RequiresPermissions("waterPrice:WiOrgbookR:edit")
    @Log(title = "机构缴费流水账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WiOrgbookR wiOrgbookR) {
        return toAjax(wiOrgbookRService.updateWiOrgbookR(wiOrgbookR));
    }

    /**
     * 删除机构缴费流水账
     */
    @RequiresPermissions("waterPrice:WiOrgbookR:remove")
    @Log(title = "机构缴费流水账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{feeYrs}")
    public AjaxResult remove(@PathVariable String[] feeYrs) {
        return toAjax(wiOrgbookRService.deleteWiOrgbookRByFeeYrs(feeYrs));
    }
}
